<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Requests\UserInsertRequest;
use App\user_detail;
use App\User;
use Hash;
use DB;
class homeUserController extends Controller
{
    /**
     * 前台登陆页面
     * @return [type] [description]
     */
    public function login(Request $request)

    {
        //获取用户名
        // $userName = $request->userName;
        //  $value = session('id');
        //    //获取用户的id
        //   $id = DB::table('users')->where('userName',$userName)->value('id');
        //  if($id == $value){
              return view('homes.login');
         // }else{
         //    return redirect('/home/index');
         // }
         
       
     }
 

    /**
     * 检测数据库  成功到个人首页CustomerIndex.html 失败返回到登陆页面
     * 前台登陆执行登陆方法
     */
    public function doLogin(Request $request)
    {

        //获取用户名
        $userName = $request->userName;
        //获取用户的id
        // $id = DB::table('users')->where('userName',$userName)->value('id');
        //读取数据库
        $user = User::where('username',$userName)->first();
        $rstatus = DB::table('users')->where('userName',$userName)->value('rstatus');

        //判断
        if(empty($user)) {
            return back()->with('info','用户名不存在');
        }
       
        // $is_check = $request->checkbox;
        // if($is_check ==1){
        //     session(['id' => $user->id]);
        // }else{
        //      session(['id' => $user->id]);
        // }

        //检测密码
        if(($rstatus ==1) && Hash::check($request->password, $user->password)) {
           
              session(['id' => $user->id]);
            
            //检验成功跳转到个人首页
            return redirect('/home/index');
			
        }else{
            return back()->with('info','密码不正确');
        }



    }

    /**
     * 前台注册显示页面
     */

    public function register()
    {
        return view('homes.register');
    }
    /**
     * 前台注册执行方法
     */
    public function doRegister(UserInsertRequest $request)
    {
        //获取用户输入的信息
        $user = new User;

        $userName = $user -> userName = $request->userName;

        $password = $user -> password = Hash::make($request->input('password'));
        // var_dump($password);die;
        $email= $user -> email = $request -> email;
        $yzm = $user->yzm = $request->yzm;
        $verify = $user->verify = $request->verify;
        if($verify == $yzm){




            //将数据用事务处理插入users和user_detail数据表中['password' => $password]
            DB::beginTransaction();
            $res_1 = DB::table('users')->insert([
                    'userName' => $userName,
                    'password' => $password,
                    'lastLogin'=> time()
                ]);

            //获取用户的id
            $id = DB::table('users')->where('userName',$userName)->value('id');
            // dd($res);die;
              $res_2 = DB::table('user_detail')->insert([
                        'email' => $email,
                        'nickName'=>$userName,
                        'birth'=>'7月7',
                        'star'=>'巨蟹',
                        'id'=>$id
                    ]);
              
          if($res_1 && $res_2) {

                DB::commit();

                echo '<!DOCTYPE html>
                <html lang="en">
                <head>
                    <meta charset="UTF-8">
                    <title>注册成功</title>
                </head>
                <body>
                    <h1>注册成功一秒后跳转注册页面....</h1>
                    <script>
                        setInterval(function(){
                            location.href="/login";
                        },1000);
                    </script>
                </body>
                </html>';

            }else{
                DB::rollBack();
                return back();
            }
        }else{
            return back()->with('info','验证码不一致');
        }
    }

}
